<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Home extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		$d['slide'] = $this->d->rows(array('table' => 'slide', 'where' => 'status=1', 'order' => 'sort asc'));
		$class = $this->d->rows(array('table' => 'article_class', 'where' => 'mod="list"', 'limit' => '2'));

		foreach ($class as $k => $v) {
			$class[$k]['article'] = $this->d->rows(array('table' => 'article', 'where' => 'cid=' . $v['id']));
		}

		$d['class'] = $class;
		$d['partner'] = $this->d->rows(array('table' => 'partner', 'where' => 'status=1', 'order' => 'sort asc'));
		$d['list'] = $this->d->rows(array('table' => 'items', 'where' => 'status=2 or status=7 or status=3', 'order' => 'add_time desc'));

		foreach ($d['list'] as $k => $v) {
			$d['list'][$k]['coin'] = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $v['coin_id']));
			$d['list'][$k]['percent'] = 0 < $v['target_amount'] ? num(($v['deal'] / $v['target_amount']) * 100, 4) : 0;
			$d['list'][$k]['name'] = mb_substr($v['name'], 0, 12, 'utf-8');
			$d['list'][$k]['intro'] = mb_substr($v['intro'], 0, 70, 'utf-8');
			$d['list'][$k]['target_amount'] = num($v['target_amount']);
			$d['list'][$k]['deal'] = num($v['deal']);
			$d['list'][$k]['day'] = $v['begin'] < $v['end'] ? intval(($v['end'] - $v['begin']) / (24 * 3600)) : 0;

			if (time() < $v['begin']) {
				$d['list'][$k]['stat'] = 0;
			}
			else {
				if (($v['begin'] <= time()) && (time() < $v['end'])) {
					if ($v['is_over'] == 1) {
						$d['list'][$k]['stat'] = 1;
						$d['list'][$k]['day'] = time() < $v['end'] ? intval(($v['end'] - time()) / (24 * 3600)) : 0;
					}
					else if ($v['target_amount'] <= $v['deal']) {
						$d['list'][$k]['stat'] = 2;
						$d['list'][$k]['day'] = 0;
					}
					else {
						$d['list'][$k]['stat'] = 1;
						$d['list'][$k]['day'] = time() < $v['end'] ? intval(($v['end'] - time()) / (24 * 3600)) : 0;
					}
				}
				else {
					$d['list'][$k]['stat'] = 2;
				}
			}
		}

		$this->show(array('header', 'home', 'footer'), $d);
	}
}

?>
